
package com.yeyks.common.utils;

import com.google.common.base.Preconditions;

import java.util.regex.Pattern;

import static com.google.common.base.Preconditions.checkState;


/**
 * @author fuchangshuai
 * @time 2018/02/21
 */
public enum TextValidator {

    EMAIL(){
        private Pattern pattern=Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");

        @Override
        public boolean boolCheck(String text) {
            return text != null && pattern.matcher(text).matches();
        }
    },

    MOBILE(){
        private Pattern pattern=Pattern.compile("^(1)\\d{10}$");

        @Override
        public boolean boolCheck(String text) {
            return text != null && pattern.matcher(text).matches();
        }
    },
    
    ID(){
    	private Pattern pattern=Pattern.compile("^\\d+$");

        @Override
        public boolean boolCheck(String text) {
            return text != null && pattern.matcher(text).matches();
        }
    },
    DIGITAL(){
    	private Pattern pattern=Pattern.compile("^\\d+$");

        @Override
        public boolean boolCheck(String text) {
            return text != null && pattern.matcher(text).matches();
        }
    },

    /**
     * 用户名长度 4~32
     * 长度4——20位，只能为数字、字母、特殊符号_ 组成
     */
    USERNAME() {
        private Pattern pattern = Pattern.compile("^(?!(1)\\d{10})[\\d_a-zA-Z]{4,32}$");

        @Override
        public boolean boolCheck(String text) {
            return text != null && pattern.matcher(text).matches();
        }
    },

    /**
     * 密码长度限制 8~16
     * 长度8——16位，不能为全数字
     */
    PASSWORD() {
        private Pattern pattern = Pattern.compile("(?![0-9]+$)[\\x21-\\x7e]{8,16}");

        @Override
        public boolean boolCheck(String text) {
            return text != null && pattern.matcher(text).matches();
        }
    },

    /**
     * 商品标题限制 1~200
     */
    ITEM_TITLE() {
        private Pattern pattern = Pattern.compile("^.{1,200}$");

        @Override
        public boolean boolCheck(String text) {
            return text != null && pattern.matcher(text).matches();
        }
    },

    /**
     * 物流编码 6~32
     */
    LOGISTIC_NO() {
        private Pattern pattern = Pattern.compile("^.{6,32}$");

        @Override
        public boolean boolCheck(String text) {
            return text != null && pattern.matcher(text).matches();
        }
    },

    /**
     * 资源角色名 1~20
     */
    RES_ROLE() {
        private Pattern pattern = Pattern.compile("^[\\d_a-zA-Z\\u4E00-\\u9FA5]{1,20}$");

        @Override
        public boolean boolCheck(String text) {
            return text != null && pattern.matcher(text).matches();
        }
    },

    /**
     * 子域名规则
     * http://stackoverflow.com/a/7933253
     */
    SUB_DOMAIN() {
        private Pattern pattern = Pattern.compile("^[A-Za-z0-9](?:[A-Za-z0-9\\-]{0,61}[A-Za-z0-9])?$");

        @Override
        public boolean boolCheck(String text) {
            return text != null && pattern.matcher(text).matches();
        }
    };

    abstract public boolean boolCheck(String text);

    public final String check(String text, String error) throws IllegalStateException {
        Preconditions.checkState(boolCheck(text), error);
        return text;
    }

    public final String check(String text) throws IllegalStateException {
        return check(text, null);
    }
}
